package likouJava;

import java.util.ArrayList;
import java.util.List;

public class 划分字母区间 {
    public List<Integer> partitionLabels(String s) {
        int[] last = new int[26];
        int n = s.length();

        //记录每个字母最后出现的位置
        for (int i = 0; i < n; i++) {
            last[s.charAt(i) - 'a'] = i;
        }

        List<Integer>res = new ArrayList<>();
        int start = 0;
        int end = 0;

        //贪心算法计算
        for (int i = 0; i < n; i++) {
            end = Math.max(end, last[s.charAt(i) - 'a']);
            if (i == end){
                //这一段结束
                res.add(end - start + 1);
                start = end + 1;
            }
        }
        return res;
    }


}
